﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using olierFrm.Extensions.EString;
using System.Collections;

namespace olierFrm.MVC
{
    public class ActionPageParams
    {
        public class Post : Hashtable
        {
            public string SqlID
            {
                get;
                set;
            }
            public string ToUrl
            {
                get;
                set;
            }
            public Post(string SqlID, string ToUrl)
            {
                this.SqlID = SqlID;
                this.ToUrl = ToUrl;
                this.GetParams();
            }
            public Post(System.Web.Routing.RouteData routeData)
            {
                this.SqlID = routeData.Values["SqlID"].ToString();
                this.ToUrl = routeData.Values["ToURL"].ToString();
                this.GetParams();
            }
        
            private void GetParams()
            {
                NameValueCollection pars = System.Web.HttpContext.Current.Request.Form;
                Regex detailRegex = new Regex("^\\[\\w+\\]\\[\\d+\\]\\[\\d+\\]$");
                Regex masterRegex = new Regex("^\\[\\w+\\]\\[0]$");
                Regex reField = new Regex("\\[\\w+\\]");
                Regex reIdx = new Regex("\\[\\d+\\]");
                List<object> ListParam = new List<object>();
                this.Add(this.SqlID, ListParam);
                for (int i = 0; i < pars.Count; i++)
                {
                    if (pars.Keys[i] == null || pars.Keys[i].Length == 0) continue;
                    string key = pars.Keys[i];
                    if (masterRegex.IsMatch(key))
                    {
                        if (ListParam.Count == 0)
                        {
                            ListParam.Add(new Dictionary<string, object>());
                        }
                        Dictionary<string, object> masterField = (Dictionary<string, object>)ListParam[0];
                        masterField.Add(reField.Match(key).Groups[0].Value.Slice(1, -1), pars[key]);
                    }
                    else
                    {
                        if (detailRegex.IsMatch(key))
                        {
                            int businessIdx = Convert.ToInt32(reIdx.Matches(key)[0].Value.Slice(1, -1));
                            if (ListParam.Count == businessIdx)
                            {
                                ListParam.Add(new List<object>());
                            }
                            List<object> detailField = (List<object>)ListParam[businessIdx];
                            int rowId = Convert.ToInt32(reIdx.Matches(key)[1].Value.Slice(1, -1));
                            if (detailField.Count == rowId)
                            {
                                detailField.Add(new Dictionary<string, object>());
                            }
                            Dictionary<string, object> field = (Dictionary<string, object>)detailField[rowId];
                            field.Add(reField.Matches(key)[0].Value.Slice(1, -1), pars[key]);
                        }
                    }
                }
            }
        }

        [Serializable]
        public class Get
        {
            public string CustController
            {
                get;
                set;
            }
            public string Module
            {
                get;
                set;
            }
            public Get(System.Web.Routing.RouteData routeData)
            {
                this.CustController = routeData.Values["CustController"] == null ? "" : routeData.Values["CustController"].ToString();
                this.Module = routeData.Values["Module"] == null ? "" : routeData.Values["Module"].ToString();
            }
        }
    }
}
